package com.lanxin.Login.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanxin.Login.service.LoginService;
import com.lanxin.hr.entity.ZzAccount;
import com.lanxin.hr.entity.ZzEmployee;
import com.lanxin.hr.service.IZzEmployeeService;
import com.lanxin.judge.Vo.ApplyVo;
import com.lanxin.judge.service.ILcApprovalService;
import com.lanxin.hr.service.IZzAccountService;
import com.lanxin.xx.entity.XxInformation;
import com.lanxin.xx.service.IXxDuanxiaoxiService;
import com.lanxin.xx.service.IXxInformationService;
import com.lanxin.xx.vo.Duanxiaoxi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-16
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService accountService;
    @Autowired
    private IZzAccountService accountService1;

    @Autowired
    private IZzEmployeeService employeeService;

    @Autowired
    private ILcApprovalService approvalService;

    @Autowired
    private IXxInformationService xxInformationService;

    @Autowired
    private IXxDuanxiaoxiService xxDuanxiaoxiService;

    //!!!!!!！！！！！！！！！！!!这里有我的代码@！！！！！！！！！！！！
    // 登录验证
    @RequestMapping("/check")
    public String check(ZzAccount account, HttpSession session, Model model) {
        // 1. 验证登录
        if(accountService.login(account)!=null){
            QueryWrapper<ZzAccount> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", account.getUsername());
            queryWrapper.eq("password", account.getPassword());
            ZzAccount myaccount = accountService1.getOne(queryWrapper);

            // 2. 获取登录的员工信息（包含员工ID）
            ZzEmployee employee = employeeService.getEmpById(
                    accountService.login(account).getAid() // 这里获取的是员工ID对应的员工信息
            );

            // 3. 关键：将员工ID存入Session，键为empOid（与其他控制器的@SessionAttribute对应）
            session.setAttribute("empOid", employee.getOid()); // 存储员工ID（empOid）
            session.setAttribute("myaccount", myaccount);
            session.setAttribute("empName", employee.getEmpName());
            session.setAttribute("account", accountService.login(account));
            model.addAttribute("name", employee.getEmpName());

            return "index";
        }
        // 登录失败，返回登录页
        return "../../login";
    }


    //left初始化
    @RequestMapping("/gotoLeft")
    public String gotoLeft(){
        return "left";
    }

    //!!!!!!！！！！！！！!!这里有我的代码@！！！！！！！！！！！！
    //home初始化
    @RequestMapping("/gotoHome")
    public String gotoHome(HttpSession session,Model model){
        ZzAccount account = (ZzAccount) session.getAttribute("account");
        // 获取当前登录员工ID（从Session中获取）
        Integer empOid = (Integer) session.getAttribute("empOid");
        //获取我的待审批工作
        List<ApplyVo> myApprovals = approvalService.getMyApprovals(account);
        session.setAttribute("myApprovals", myApprovals);
        model.addAttribute("myApprovals", myApprovals);

        // 2. 公司新闻（类型1）和通知公告（类型2）
        List<XxInformation> newsList = xxInformationService.getLatestInformation(1, 5);
        List<XxInformation> noticeList = xxInformationService.getLatestInformation(2, 5);
        model.addAttribute("newsList", newsList);
        model.addAttribute("noticeList", noticeList);

        // 3. 内部短消息（使用从Session获取的empOid）
        if (empOid != null) {
            List<Duanxiaoxi> recentMsgs = xxDuanxiaoxiService.getRecentMsgs(empOid, 5);
            model.addAttribute("recentMsgs", recentMsgs);
        } else {
            // 处理未获取到empOid的异常情况（如跳转登录页）
            return "redirect:/login";
        }

        return "home";
    }
}
